package com.gebogebo.android.pub;

import java.io.File;
import java.io.FileOutputStream;
import java.util.UUID;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class createDoddleActivity extends Activity implements OnClickListener, OnSeekBarChangeListener {
	
    private SeekBar		BrushSizeSeekBar;
    private TextView	BrushSizeSeekBarProgressText;
    private pubView		mPubView;
    private String		CustomerSelectedImageName;
    private String		CustomerPickedColor;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.createdoodlelayout);
        
        mPubView = (pubView)findViewById(R.id.pubView1);
        
        BrushSizeSeekBar = (SeekBar)findViewById(R.id.seekbar1);
		BrushSizeSeekBarProgressText = (TextView)findViewById(R.id.brushsizetextid);
		
		Log.v("Brush Text", (BrushSizeSeekBarProgressText != null)? BrushSizeSeekBarProgressText.getText().toString() : "is null");
		
		BrushSizeSeekBarProgressText.setText("Brush size="+mPubView.getBrushSize());
		BrushSizeSeekBar.setProgress((int)mPubView.getBrushSize());
		BrushSizeSeekBar.setOnSeekBarChangeListener(this);
		findViewById(R.id.savebutton).setOnClickListener(this);
		findViewById(R.id.pickcolor).setOnClickListener(this);
		
		CustomerSelectedImageName = null;
    }
    public void onClick(View v){
		Log.v("Inline onClick","True");
		int id = v.getId();
		Intent myIntent;
		switch(id){
		case R.id.savebutton:
			Log.v("Button press","Parce");
			
			String GUID = UUID.randomUUID().toString();
			myIntent = getIntent();
			String FileNameToUse;
			String BaseSperator = "/";
			String BaseName = "pub-";
			String ExtName = ".png";
			
			mPubView.setBitmaptoAlpha();
			
			if(CustomerSelectedImageName != null){
				FileNameToUse = CustomerSelectedImageName;
			}else{
				FileNameToUse = BaseName+GUID+ExtName;
			}
			
			Log.v("Attempting to create",getFilesDir()+FileNameToUse);
			
			FileOutputStream fos = null;
			try{
				
				File ImageFile = new File(getFilesDir(),FileNameToUse);
				
				Log.v("File existis",(ImageFile.exists())? "Yes"+ImageFile.toString():"No");
				
				fos = new FileOutputStream(ImageFile);
				mPubView.getBitmap().compress(CompressFormat.PNG, 20, fos);
				
				myIntent.putExtra(getString(R.string.pubviewviewid), getFilesDir()+"/"+FileNameToUse);
				
				fos.close();
			}catch (Exception e){
				Log.v("Parse Steps","Unable to open file stream");
				e.printStackTrace();
			}
			Log.v("Parse Steps","Finished try");
			setResult(Constants.SAVE_CREATED_DOODLE_CODE,myIntent);
			finish();
			break;
		case R.id.pickcolor:
			Log.v("Button press","Pick Color pressed");
			//This was suppose to be used for pop-up name
			//Intent GetImageNameIntent = new Intent(this, GetImageNameActivity.class);
			//startActivityForResult(GetImageNameIntent,Constants.GET_IMAGE_NAME_CODE);
			
			Intent PickColorIntent = new Intent(this, PickYourColorDialogActivity.class);
			startActivityForResult(PickColorIntent,Constants.GET_COLOR_SELECTED);
			
			break;
		default:
			Log.v("Button press","Default");
			break;
		}
	}
    
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		Bundle extras;
		
		switch(requestCode){
		case Constants.GET_IMAGE_NAME_CODE:
			extras = (data != null)? data.getExtras() : null;
			Log.v("Image Name","We got an image name back");
			
			if (extras != null && extras.containsKey(getString(R.string.imagefilenametext))){				
				CustomerSelectedImageName = extras.getString(getString(R.string.imagefilenametext));
				Log.v("Image Name is",CustomerSelectedImageName);
			}
			break;
		case Constants.GET_COLOR_SELECTED:
			extras = (data != null)? data.getExtras() : null;
			if (extras != null && extras.containsKey("ColorPicked")){				
				CustomerPickedColor = extras.getString("ColorPicked");
				Log.v("Color Selected",CustomerPickedColor);
			}
			mPubView.setPaintBrushColor(CustomerPickedColor);
			break;
		default:
			Log.v("Image Name","Code is not right");
			break;
		}
	}
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
		// TODO Auto-generated method stub
		
		BrushSizeSeekBarProgressText.setText("Brush size="+progress);
		mPubView.setBrushSize(progress);
		
		Log.v("Bar Change",String.valueOf(progress));
    }

	public void onStartTrackingTouch(SeekBar seekBar){
		// TODO Auto-generated method stub
    }

	public void onStopTrackingTouch(SeekBar seekBar){
		// TODO Auto-generated method stub
	}
	
	
}

